package com.taptech.doufu.ui.adapter;

import android.content.Context;
import android.content.Intent;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NewAlbumsActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.DFHomeCategoryDetailBean;
import com.taptech.doufu.util.GlideRoundTransform;
import com.taptech.doufu.util.GlideUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13/013.
 */
public class ChoiceCategoryAdapter extends RecyclerView.Adapter<ChoiceCategoryAdapter.ViewHolder>{

    private List<DFHomeCategoryDetailBean> list;
    private Context context;
    public ChoiceCategoryAdapter(Context context, List<DFHomeCategoryDetailBean> list){
        this.list = list;
        this.context = context;

    }

    public DFHomeCategoryDetailBean getItem(int position){
        if (list != null && list.size()>position) {
            return list.get(position);
        }
        return null;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_choice_category, null);
        ViewHolder holder = new ViewHolder(view);
        holder.tvTitle = view.findViewById(R.id.tvTitle);
        holder.ivPic = view.findViewById(R.id.ivPic);
        holder.lay = view.findViewById(R.id.lay);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final DFHomeCategoryDetailBean model = getItem(position);
        if (model != null) {
            holder.tvTitle.setText(model.getTitle());
            if (!TextUtils.isEmpty(model.getImgUrl())) {
                GlideUtil.displayRoundImage(context,holder.ivPic, model.getImgUrl(),3,R.drawable.img_goods_default_loading_xhdpi);
            }

            holder.lay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, NewAlbumsActivity.class);
                    intent.putExtra(Constant.URL, model.getUrl());
                    intent.putExtra(Constant.NAME, model.getTitle());
                    context.startActivity(intent);

                }
            });
            changeDayModelBg(holder);
        }
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvTitle;
        ImageView ivPic;
        View lay;
        public ViewHolder(View itemView) {
            super(itemView);

        }
    }

    private void changeDayModelBg(ViewHolder viewHolder){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            darkBg(viewHolder);
        }else {
            dayBg(viewHolder);
        }
    }

    private void dayBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.all_activity_bg));
        viewHolder.tvTitle.setTextColor(context.getResources().getColor(R.color.text_color_22));
        viewHolder.ivPic.setAlpha(1.0f);
    }

    private void darkBg(ViewHolder viewHolder) {
        viewHolder.lay.setBackgroundColor(context.getResources().getColor(R.color.fg_dark));
        viewHolder.tvTitle.setTextColor(context.getResources().getColor(R.color.title_text_dark));
        viewHolder.ivPic.setAlpha(Constant.imagViewAlpha);
    }
}
